package irio.pythcal;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * A simple wrapper around a Camera and a SurfaceView that renders a centered preview of the Camera
 * to the surface. We need to center the SurfaceView because not all devices have cameras that
 * support preview sizes at the same aspect ratio as the device's display.
 */
class Preview extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {  
    SurfaceHolder mHolder;  
  
    Camera mCamera = null;  
    protected boolean cameraEnabled = false;

    //This variable is responsible for getting and setting the camera settings  
    private Parameters parameters;  
    //this variable stores the camera preview size  
    private Size previewSize;  
    //this array stores the pixels as hexadecimal pairs  
    private SurfaceHolder holder;
    
    Preview(Context context) {  
        super(context);  
  
        // Install a SurfaceHolder.Callback so we get notified when the  
        // underlying surface is created and destroyed.  
        mHolder = getHolder();  
        mHolder.addCallback(this);  
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
    }  
  
    
    public void surfaceCreated(SurfaceHolder holder) {  
    	this.holder = holder;
      }
  
    public void surfaceDestroyed(SurfaceHolder holder) {  
    	stop();
    }  
  
    public void stop() {
    	if (mCamera==null)
    		return;
    	mCamera.setPreviewCallback(null);
    	mCamera.stopPreview();
    	mCamera.release();  
    	mCamera = null;
        cameraEnabled = false;
    }

    public void start() {
        mCamera = Camera.open();  
        //set the camera's settings  
     try {  
           mCamera.setPreviewDisplay(holder);  
  
           //sets the camera callback to be the one defined in this class  
           mCamera.setPreviewCallback(this);  
           mCamera.setDisplayOrientation(90);
           ///initialize the variables  
           parameters = mCamera.getParameters();  
           previewSize = parameters.getPreviewSize();  
           //pixels = new int[previewSize.width * previewSize.height];  
  
        } catch (IOException exception) {  
            mCamera.release();  
            //mCamera = null;  
            // TODO: add more exception handling logic here  
        }  
    	//parameters.setPreviewSize(previewSize.width, previewSize.height);  
    	parameters.setPreviewSize(352,288);  
        mCamera.setParameters(parameters);  
    	mCamera.startPreview();  
    }

    
    
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {  
        // Now that the size is known, set up the camera parameters and begin  
        // the preview.  
        //parameters.setPreviewSize(352,288);  
        //parameters.setPreviewSize(352,288);  
        //parameters.setPreviewSize(w,h);  
    
    	//parameters.setPreviewSize(352/2,288/2);  
        //set the camera's settings  
        if (mCamera!=null) {
        	parameters.setPreviewSize(w,h);  
            mCamera.setParameters(parameters);  
        	mCamera.startPreview();  
        }
    }  
  
    public void onPreviewFrame(byte[] data, Camera camera) {  
        //transforms NV21 pixel data into RGB pixels  
        //decodeYUV420SP(pixels, data, previewSize.width,  previewSize.height);  
        //Outuput the value of the top left pixel in the preview to LogCat  
        //Log.i("Pixels", "The top right pixel has the following RGB (hexadecimal) values:"  
        //        +Integer.toHexString(pixels[0]));  
    }  
  
  
}  